<title>发放工资 - 工资结算管理</title>

<script src="lib/assets/js/dataTables/jquery.dataTables.js"></script>
<script src="lib/assets/js/dataTables/jquery.dataTables.bootstrap.js"></script>
<script src="lib/assets/js/dataTables/extensions/TableTools/js/dataTables.tableTools.js"></script>

<div class="page-header">
	<h1>
		工资管理 <small> <i class="ace-icon fa fa-angle-double-right"></i>
			发放工资
		</small>
	</h1>
</div>

<div class="row">
	<div class="col-xs-12">

		<div class="widget-box">
			<div class="widget-header"><h4 class="widget-title">选择查询条件</h4></div>

			<div class="widget-body" style="display: block;">
				<form id="conditionForm" action="" method="post">
					<div class="widget-main">
						<div class="row">
							<div class="col-md-4  col-md-push-1 col-lg-4 col-lg-push-1">
								<label>部门</label><select id="depList"
									class="form-control"></select> <br />
							</div>
							<div class="col-md-4  col-md-push-1  col-lg-4 col-lg-push-1">
								<label>发放月</label><select id="monthList"
									class="form-control"></select> <br />
							</div>
							<div class="col-md-3  col-md-push-1 col-lg-3 col-lg-push-1">
								<br /> <input id="query" type="submit" value="查询"
									class="btn btn-primary" /> <input type="button" value="重选"
									class="btn btn-primary"
									onclick="$('.form-control').val('null').trigger('chosen:updated')" />
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>

		<br />
		<div class="tableTools-container"></div>
		<div class="table-responsive ">
			<table id="show" class="table table-bordered table-hover text-center"></table>
			<div id="gridPager"></div>
		</div>
	</div>
</div>

<script type="text/javascript">
	var scripts = [ null, "lib/assets/js/chosen.jquery.js", null ];
	var distribute = function(i) {
		var btnId = "chgBtn" + i + "Edit";
		var empId = $("#"+btnId).children("td[name=emp_id]").html();
		$("#"+btnId).children("td:last-child").children().attr("disabled", "disabled");
		var month = $("#monthList").val();
		$.ajax({
			type : "GET",
			url : "http://localhost:8080/mysms/empsalary/distribute?empId=" + empId +"&month=" + month,
			success: function(data) {
				alert("发放成功 ^_^");
			}
		});	
	}
	
	$('.page-content-area')
			.ace_ajax(
					'loadScripts',
					scripts,
					function() {
						/* 动态加载[发放月]列表 */
						$("#monthList").append(
								"<option value=\"null\">请选择</option>");
						for (var i = 1; i < 13; i++) {
							$("#monthList").append(
									"<option value=\"" + i + "\">" + i
											+ "月</option>");
						}
						$("#monthList").chosen();

						/* 动态加载[部门]列表 */
						$
								.ajax({
									type : "GET",
									url : "http://localhost:8080/mysms/depliyi/get",
									success : function(data) {
										var options = data.response;
										$("#depList").empty();
										$("#depList")
												.append(
														"<option value=\"null\">请选择</option>");
										for (var i = 0; i < options.length; i++) {
											var option = options[i];
											$("#depList").append(
													"<option value=\"" + 
									option.dep_id + "\">"
															+ option.dep_name
															+ "</option>");
										}
										$("#depList").chosen();
									}
								});

						/* 查询参数为空的表单验证 和 异步加载查询结果 */
						$("#conditionForm")
								.bind(
										"submit",
										function() {
											var err = "";
											if ($("#depList").val() == "null") {
												err += "-- 亲，请指定 [部门] 信息 -- \n";
											}
											if ($("#monthList").val() == "null") {
												err += "- 亲，请指定 [发放月] 信息 - \n";
											}
											if (err == "") {
												/* 异步加载查询结果 */
												var depId = $("#depList").val();
												var month = $("#monthList")
														.val();
												$
														.ajax({
															type : "GET",
															url : "http://localhost:8080/mysms/empsalary/get?depId="
																	+ depId
																	+ "&month="
																	+ month,
															success : function(
																	data) {
																var trs = data.response;
																
																if (typeof(tbplug)!="undefined") {
																	tbplug.destroy();
																}
																
																$("#show")
																		.empty();
																for (var i = 0; i < trs.length; i++) {
																	var salary = trs[i];
																	var append;
																	/* 生成表头 */
																	if (i == 0) {
																		append = "<thead>"
																				+ "<tr>";
																		for (key in salary) {
																			append += "<th>"
																					+ salary[key]
																					+ "</th>";
																		}
																		append += "<th>操作</th></tr>"
																				+ "</thead><tbody>"
																	}
																	/* 生成数据 */
																	else {
																		append = "<tr id='chgBtn" + i + "Edit'>";
																		for (key in salary) {
																			append += "<td name=" + key + ">"
																					+ salary[key]
																					+ "</td>";
																		}
																		append += "<td><input type='button' class='btn btn-success btn-xs' value='发放' onclick='distribute(" + i + ")' /></td></tr>";
																	}
																	$("#show")
																			.append(
																					append);
																}
																$("#show")
																		.append(
																				"</tbody>");
																
																var tbplug = $("#show").DataTable({
																	destroy: true
																});
																
															}
														});
												return false;
											} else {
												alert(err);
												return false;
											}
										});
					});
</script>